package com.dense.kuiniu.floatcabinet.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.dense.kuiniu.floatcabinet.R;
import com.dense.kuiniu.floatcabinet.di.component.DaggerHomeComponent;
import com.dense.kuiniu.floatcabinet.di.module.HomeModule;
import com.dense.kuiniu.floatcabinet.mvp.contract.HomeContract;
import com.dense.kuiniu.floatcabinet.mvp.event.BackResultEvent;
import com.dense.kuiniu.floatcabinet.mvp.event.BorrowResultEvent;
import com.dense.kuiniu.floatcabinet.mvp.event.InventoryResultEvent;
import com.dense.kuiniu.floatcabinet.mvp.event.OpenDoorEvent;
import com.dense.kuiniu.floatcabinet.mvp.event.ScanEvent;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.AdminBean;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.Reader;
import com.dense.kuiniu.floatcabinet.mvp.manager.bord.Door;
import com.dense.kuiniu.floatcabinet.mvp.manager.bord.DoorManager;
import com.dense.kuiniu.floatcabinet.mvp.presenter.HomePresenter;
import com.dense.kuiniu.floatcabinet.mvp.ui.adapter.BannerAdapter;
import com.dense.kuiniu.floatcabinet.mvp.ui.adapter.BookTagAdapter;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.DialogAdminLogin;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.DialogDoorNotClose;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.DialogPutCard;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.jess.arms.di.component.AppComponent;
import com.zhpan.bannerview.BannerViewPager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2020/9/28
 */
public class HomeActivity extends ImmerseActivity<HomePresenter> implements HomeContract.View {

    @BindView(R.id.banner)
    BannerViewPager<Integer, BannerAdapter.NetViewHolder> mBannerViewPager;
    @BindView(R.id.recycler_view_recommend)
    RecyclerView mRvRecommend;
    private BookTagAdapter mBookTagAdapter;
    private DialogPutCard dialogPutCard;
    private DialogDoorNotClose dialogDoorNotClose;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerHomeComponent.builder()
                .appComponent(appComponent)
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_home;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initDialog();
        initBanner();
        initRecyclerView();
        if (mPresenter != null) {
            mPresenter.startExecuteUnfinishedTask();
        }
    }


    @Override
    public void showMessage(@NonNull String message) {

    }


    @OnClick({R.id.tv_query, R.id.tv_borrow, R.id.iv_admin, R.id.tv_back})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_query:
                startActivity(new Intent(this, QueryActivity.class));
                break;
            case R.id.iv_admin:
                new DialogAdminLogin(this, (user, password) -> {
                    startActivity(new Intent(this, AdminActivity.class));
                }).show();
                break;
            case R.id.tv_borrow:
                if (checkDoors()) {
                    dialogPutCard.show(cardNo -> {
                        if (mPresenter != null) {
                            mPresenter.authentication(cardNo, BorrowActivity.class);
                        }
                    });
                }
                break;
            case R.id.tv_back:
                if (checkDoors()) {
                    dialogPutCard.show(cardNo -> {
                        if (mPresenter != null) {
                            mPresenter.back(cardNo);
                        }
                    });
                }
                break;
        }
    }

    private void initDialog() {
        dialogPutCard = new DialogPutCard(this);
        dialogDoorNotClose = new DialogDoorNotClose(this);
    }

    private void initBanner() {
        mBannerViewPager.setAutoPlay(true)
                .setOrientation(ViewPager2.ORIENTATION_HORIZONTAL)
                .setInterval(10 * 1000)
                .setAdapter(new BannerAdapter()).create(new ArrayList<Integer>() {{
            add(R.mipmap.bg_test_banner);
        }});
    }

    private void initRecyclerView() {
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setAlignItems(AlignItems.STRETCH);
        mRvRecommend.setLayoutManager(layoutManager);
    }


    @Override
    public void adminLoginResult(AdminBean adminBean) {

    }

    @Override
    public void authentication(Reader reader, Class<?> cls) {
        Intent intent = new Intent(this, cls);
        intent.putExtra("cardNo", reader.getVuserInfo().getAccount());
        startActivity(intent);
    }

    @Subscribe
    public void onOpenDoorAction(OpenDoorEvent event) {
        if (mPresenter != null) {
            mPresenter.openDoor(event.account, event.doorNumber, event.isReader);
        }
    }

    @Subscribe
    public void onInventoryAction(ScanEvent event) {
        if (mPresenter != null) {
            Log.e("onInventoryAction", "onInventoryAction");
            mPresenter.scan(event.account, event.doorNumber);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 0)
    public void borrowResult(BorrowResultEvent event) {
        Log.e("borrowResult", "借书成功");
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 0)
    public void backResult(BackResultEvent event) {
        Log.e("backResult", "还书成功");
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 0)
    public void inventoryResult(InventoryResultEvent event) {
        Log.e("inventoryResult", "盘点成功");
    }

    private boolean checkDoors() {
        List<Door> openDoors = DoorManager.getInstance().getOpenDoorList();
        if (!openDoors.isEmpty()) {
            dialogDoorNotClose.updateInfo(openDoors);
            dialogDoorNotClose.show();
            return false;
        }
        return true;
    }
}